//
//  InstructionView.m
//  InstructionView
//
//  Created by 李亚军 on 17/2/17.
//  Copyright © 2017年 李亚军. All rights reserved.
//

#import "InstructionView.h"

@interface InstructionView ()


@property(nonatomic , strong) CAShapeLayer *shapeLayer;

@end

IB_DESIGNABLE
@implementation InstructionView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self configureTrianglePoint];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    
    [self configureTrianglePoint];
    
    self.shapeLayer = [CAShapeLayer layer];
    [self.layer addSublayer: self.shapeLayer];
}

- (void)configureTrianglePoint {
    
    self.trianglePoint = CGPointMake(30, self.frame.size.height);
    
    [self configureDefaultAttributes];
    
    //    self.shapeLayer = [CAShapeLayer layer];
    
    // 设置绘图对象属性
    self.shapeLayer.frame = self.bounds;
    self.shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
    self.shapeLayer.lineWidth = self.lineWidth; // 设置线宽
    self.shapeLayer.fillColor = self.fillColor.CGColor;
    self.shapeLayer.strokeColor = self.lineColor.CGColor;
    self.shapeLayer.path = [[self drawGraphical] CGPath];
    self.shapeLayer.lineJoin = kCALineJoinRound;
}




- (void)drawRect:(CGRect)rect {
    

}


/**
 配置默认属性
 */
- (void)configureDefaultAttributes {
    
    if (self.lineColor == nil) {
        self.lineColor = [UIColor clearColor];
    }
    
    if (self.fillColor == nil) {
        self.fillColor = [UIColor clearColor];
    }
}


/**
 绘画图形
 */
- (UIBezierPath *)drawGraphical {
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    
//    bezierPath.lineCapStyle = kCGLineJoinRound;
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    // 右上角圆角中线点
    CGPoint topRightCornerPoint = CGPointMake(width - self.rectangleFillet - self.lineWidth / 2.0, self.rectangleFillet + self.lineWidth / 2.0);
    // 右下角圆角中线点
    CGPoint bottomRightCornerPoint = CGPointMake(width - self.rectangleFillet - self.lineWidth / 2.0, height - self.rectangleFillet - self.lineWidth / 2.0 - self.triangleHeight);
    // 左下角圆角中线点
    CGPoint bottomLeftCornerPoint = CGPointMake(self.lineWidth / 2.0 + self.rectangleFillet, height - self.rectangleFillet - self.lineWidth / 2.0 - self.triangleHeight);
    // 左上角圆角中线点
    CGPoint topLeftCornerPoint = CGPointMake(self.lineWidth / 2.0 + self.rectangleFillet, self.rectangleFillet + self.lineWidth / 2.0);
    // 绘制三角形的起始点
    CGPoint originPoint = CGPointMake(self.trianglePoint.x + self.triangleHeight * tan(M_PI / 6), height - self.triangleHeight - self.lineWidth / 2.0);
    
    [bezierPath moveToPoint:originPoint];
    [bezierPath addArcWithCenter:CGPointMake(self.trianglePoint.x, height - self.lineWidth / 2.0 - self.triangleFillet) radius:self.triangleFillet startAngle:M_PI / 3 endAngle:M_PI / 3 * 2 clockwise:YES];
    [bezierPath addLineToPoint:CGPointMake(self.trianglePoint.x - self.triangleHeight * tan(M_PI / 6), height - self.triangleHeight - self.lineWidth / 2.0)];
    [bezierPath addArcWithCenter:bottomLeftCornerPoint radius:self.rectangleFillet startAngle:M_PI / 2.0 endAngle:M_PI clockwise:YES];
    [bezierPath addArcWithCenter:topLeftCornerPoint radius:self.rectangleFillet startAngle:M_PI endAngle:M_PI / 2.0 * 3 clockwise:YES];
    [bezierPath addArcWithCenter:topRightCornerPoint radius:self.rectangleFillet startAngle:M_PI / 2.0 * 3 endAngle:0 clockwise:YES];
    [bezierPath addArcWithCenter:bottomRightCornerPoint radius:self.rectangleFillet startAngle:0 endAngle:M_PI / 2.0 clockwise:YES];
    [bezierPath addLineToPoint:originPoint];
    
    return bezierPath;
}

#pragma mark - 重写setter方法
- (void)setLineColor:(UIColor *)lineColor {
    
    _lineColor = lineColor;
    [self layoutIfNeeded];
    
}

- (void)setLineWidth:(CGFloat)lineWidth {
    _lineWidth = lineWidth;
    [self layoutIfNeeded];
    
}

- (void)setFillColor:(UIColor *)fillColor {
    _fillColor = fillColor;
    [self layoutIfNeeded];

}

- (void)setTriangleHeight:(CGFloat)triangleHeight {
    
    _triangleHeight = triangleHeight;
    [self layoutIfNeeded];
}

- (void)setTrianglePoint:(CGPoint)trianglePoint {
    
    _trianglePoint = trianglePoint;
    [self layoutIfNeeded];
}


@end
